﻿using System;

namespace SmallBasicFun.Analytics
{
	public class GooglePageView
	{
		public string PageTitle { get; private set; }

		public string DomainName { get; private set; }

		public string Url { get; private set; }

		/// <summary>
		/// Initializes a new instance of the <see cref="GooglePageView"/> class.
		/// </summary>
		/// <param name="pageTitle">The page title. (required)</param>
		/// <param name="domainName">domain hostname ie www.yourdomain.com (required)</param>
		/// <param name="url">The URL. (required)</param>
		public GooglePageView(string pageTitle, string domainName, string url)
		{
			PageTitle = pageTitle;
			DomainName = domainName;
			Url = url;

			Validate();
		}

		/// <summary>
		/// Validates this instance.
		/// </summary>
		public void Validate()
		{
			if (String.IsNullOrEmpty(PageTitle))
			{
				throw new ArgumentException("'PageTitle' is a required field","PageTitle");
			}
			if (String.IsNullOrEmpty(DomainName))
			{
				throw new ArgumentException("'DomainName' is a required field","DomainName");
			}
			if (String.IsNullOrEmpty(Url))
			{
				throw new ArgumentException("'Url' is a required field","Url");
			}
			if (Url.LastIndexOf("http://")>=0 || Url.LastIndexOf("www")>=0)
			{
				throw new ArgumentException("'Url' must be a relative to root URL with no www or http:// not:" + Url,"Url");
			}
		}

		public string GetPageTitleUrlEncoded()
		{
			return PageTitle != null ? Uri.EscapeDataString(PageTitle) : string.Empty;
		}
	}
}
